package com.zjy.pet.service.impl;

import com.zjy.basic.service.BaiduAiAuditService;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.LoginContext;
import com.zjy.org.domain.Employee;
import com.zjy.org.mapper.EmployeeMapper;
import com.zjy.pet.domain.Pet;
import com.zjy.pet.domain.PetDetail;
import com.zjy.pet.domain.PetOnlineAuditLog;
import com.zjy.pet.mapper.PetDetailMapper;
import com.zjy.pet.mapper.PetMapper;
import com.zjy.pet.mapper.PetOnlineAuditLogMapper;
import com.zjy.pet.service.PetService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetailMapper.updateDetailByPetId(pet);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        //获取登陆人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //根据登陆人信息的id找到员工信息
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //遍历 选中要上架的pet的id
        for (Long id:ids){
            //1.先取得对应id的对象
            Pet pet = petMapper.findById(id);
            //2.审核：百度智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());//先审核名称
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());//再审核图片
            if (boo1 && boo2){//如果审核成功（都为true）
                //2.审核通过：修改状态state=1，上架时间 - 添加审核日志
                petMapper.updateOnSaleState(id,new Date());

                //生成审核日志对象
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                //添加审核日志信息
                log.setPetId(id);
                log.setState(1);
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                //添加到数据库
                petOnlineAuditLogMapper.add(log);
            }else {//审核不通过
                //3.审核失败：添加审核日志
                //生成审核日志对象
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                //添加审核日志信息
                log.setPetId(id);
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditId(employee.getId());
                //添加到数据库
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(Long[] ids) {
        //1.修改状态，修改下架的时间
        petMapper.updateState(ids,new Date());
    }

}
